package com.lgl.scs.information.controller;

import com.lgl.scs.base.controller.BaseController;
import com.lgl.scs.information.service.FeedBackService;
import com.lgl.scsapi.system.model.PageData;
import com.lgl.scsapi.util.utils.DateUtil;
import com.lgl.scsapi.util.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 反馈信息处理
 */
@Slf4j
@RestController
@RequestMapping(value="/userIndex/feedback")
public class FeedBackController extends BaseController{
	@Resource(name="feedBackService")
	private FeedBackService feedBackService;
	@Value("${wx.upload.path}")
	private String uploadPath;

	//保存用户反馈信息
	@RequestMapping(value="saveFeedBack",produces="application/json;charset=UTF-8")
	public Object saveFeedBack() {
		PageData fbPageData = getPageData();
		if (!StringUtils.hasText(fbPageData.getString("fbContent"))){
			return "2";
		}
		int rows = feedBackService.saveFeedBack(fbPageData);
		return rows > 0 ? "0" : "1";
	}
	@RequestMapping(value="/uploadFile",produces="application/json;charset=UTF-8")
	public Object uploadFile(HttpServletRequest request,@RequestParam("file") MultipartFile file) {
		Map<String,Object> uploadResult = new HashMap<>(2);
		//如果文件不为空，写入上传路径
		if (file == null || file.isEmpty()){
			uploadResult.put("code", "1");
			return uploadResult;
		}
		String result = "0", fileName = String.valueOf(System.currentTimeMillis());
		try {
			//上传文件路径
			String imgPath = request.getParameter("imgPath");
			if (!StringUtils.hasText(imgPath)){
				imgPath = DateUtil.getNowYearMonth(null);
			}
			String fileSavePath = uploadPath + File.separator + imgPath;
			fileName = FileUtil.fileUpload(file,fileSavePath,fileName);
		}catch (Exception e){
			log.error("文件上传失败:",e);
			result = "2";
		}
		uploadResult.put("code", result);
		uploadResult.put("fileName",fileName);
		return uploadResult;
	}
	//删除文件
	@RequestMapping(value="/deleteFile",produces="application/json;charset=UTF-8")
	public String deleteFile(HttpServletRequest request) {
		String result = "0", fileName = request.getParameter("fileName");
		try {
			//删除文件路径
			String imgPath = request.getParameter("imgPath");
			if (!StringUtils.hasText(imgPath)){
				imgPath = DateUtil.getNowYearMonth(null);
			}
			String imgDelPathAndName = uploadPath + File.separator + imgPath + File.separator + fileName;
			FileUtil.deleteFile(imgDelPathAndName);
		}catch (Exception e){
			log.error("文件删除失败:",e);
			result = "2";
		}
		return result;
	}

}